/**
 * @file 018.有效的回文.cc
 * @author snow-tyan (zziywang@163.com)
 * @brief {Life is too short to learn cpp.}
 * @version 0.1
 * @date 2021-11-19
 * 
 * @copyright Copyright (c) 2021
 * 
 */

#include <iostream>
#include <string>
using std::string;

class Solution
{
public:
    bool isPalindrome(string s)
    {
        int n = s.size();
        int i = 0, j = n - 1;
        while (i <= j) {
            while (i <= j && !isalpha(s[i]) && !isdigit(s[i])) { //如果非数字和非字母 则跳过
                ++i;
            }
            while (i <= j && !isalpha(s[j]) && !isdigit(s[j])) {
                --j;
            }
            if (i <= j) { //都换成小写
                if (isupper(s[i])) {
                    s[i] += 32;
                }
                if (isupper(s[j])) {
                    s[j] += 32;
                }
                if (s[i] != s[j]) {
                    return false;
                }
            }
            ++i, --j;
        }
        return true;
    }
};

int main()
{
    string s = " A man, a plan, a canal: Panama!";
    std::cout << Solution().isPalindrome(s) << std::endl;            // true
    std::cout << Solution().isPalindrome("race a car") << std::endl; // false
    std::cout << Solution().isPalindrome("!") << std::endl;          // false
    return 0;
}